package cqrtplm.controller;

import com.alibaba.fastjson.JSONObject;
import com.hustcad.plm.pdm.file.model.dto.ContentUploadDTO;
import com.hustcad.plm.pdm.file.service.TyplmFileService;
import com.ty.basic.response.ResponseResult;
import com.ty.basic.utils.TyResultUtils;
import com.ty.security.util.TyCommonUtils;
import cqrtplm.constant.RouteConstant;
import cqrtplm.dto.OnlineAnnotationDTO;
import cqrtplm.service.RTChangeService;
import cqrtplm.vo.OnlineAnnotationVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

@RestController
@Api(tags = "问题报告,变更相关")
@RequestMapping(RouteConstant.CHANGE + "v1")
public class RTChangeController {
    @Resource
    private RTChangeService rtChangeService;
    @Resource
    private TyplmFileService typlmFileService;

    @RequestMapping("/uploadOnlineAnnotation")
    public ResponseResult uploadOnlineAnnotation(@ModelAttribute OnlineAnnotationDTO dto) {
        rtChangeService.uploadOnlineAnnotationFile(dto);
        return ResponseResult.success();
    }

    @GetMapping("/deleteOnlineAnnotationFile")
    public ResponseResult deleteOnlineAnnotationFile(String oid) {
        int i = rtChangeService.deleteOnlineAnnotationFile(oid);
        if (i == 0) {
            return ResponseResult.fail("删除失败", "请刷新后再尝试！");
        }
        return ResponseResult.success();
    }

    @RequestMapping("/selectOnlineAnnotationByOid")
    public ResponseResult<List<OnlineAnnotationVO>> selectOnlineAnnotationByOid(@RequestBody String oid) {
        List<OnlineAnnotationVO> onlineAnnotationVOS = rtChangeService.selectOnlineAnnotationByOid(oid);
        return ResponseResult.success(onlineAnnotationVOS);
    }

    @RequestMapping("/createProblemReportByAnnotation")
    public ResponseResult createProblemReportByAnnotation(@RequestBody List<String> list) throws Exception {
        rtChangeService.createProblemReportByAnnotation(list);
        return ResponseResult.success();
    }

    @GetMapping("/simpleDownLoadFile")
    public void simpleDownLoadFile(String fileOid, HttpServletResponse response) throws Exception {
        List<BigInteger> fileOidList = new ArrayList<>();
        fileOidList.add(new BigInteger(fileOid));
        typlmFileService.simpleDownLoadFile(fileOidList, response);
    }

    @PostMapping("checkBomCertPattern")
    @ApiOperation(value = "checkBomCertPattern", notes = "BOM认证检查", produces = "application/json; charset=utf-8")
    public ResponseResult checkBomCertPattern(@RequestBody LinkedHashMap<String,Object> map) throws Exception {
        String msg = this.rtChangeService.checkBomCertPattern(map);
        return ResponseResult.success(msg);
    }

    @PostMapping("queryPartInventory")
    @ApiOperation(value = "queryPartInventory", notes = "SAP库存查询", produces = "application/json; charset=utf-8")
    public ResponseResult queryPartInventory(@RequestBody LinkedHashMap<String,Object> map) throws Exception {
        List<JSONObject> result = this.rtChangeService.queryPartInventory(map);
        return ResponseResult.success(result);
    }

    @PostMapping("savePartInventory")
    @ApiOperation(value = "savePartInventory", notes = "SAP库存保存", produces = "application/json; charset=utf-8")
    public ResponseResult savePartInventory(@RequestBody LinkedHashMap<String,Object> map) throws Exception {
        try {
            return ResponseResult.success(this.rtChangeService.savePartInventory(map));
        } catch (Exception e) {
            return ResponseResult.fail("200", e.getMessage());
        }
    }

    @PostMapping("queryProductCodeStatus")
    @ApiOperation(value = "queryProductCodeStatus", notes = "查询成品码状态", produces = "application/json; charset=utf-8")
    public ResponseResult queryProductCodeStatus(@RequestBody List<JSONObject> list) throws Exception {
        try {
            return ResponseResult.success(this.rtChangeService.queryProductCodeStatus(list));
        } catch (Exception e) {
            return ResponseResult.fail("200", e.getMessage());
        }
    }


    @PostMapping({"/batchUploadFile"})
    @ApiOperation(
            value = "上传项目变更单附件",
            notes = "上传项目变更单附件",
            produces = "application/json; charset=utf-8"
    )
    public ResponseResult<List<ContentUploadDTO>> batchUploadFile(List<MultipartFile> file,
                                                                  BigInteger workFlowContainerId) {
        TyCommonUtils.requireNonNull(workFlowContainerId, "workFlowContainerId");
        List<ContentUploadDTO> contentUploadDTOS = rtChangeService.batchUploadFile(file, workFlowContainerId);
        return TyResultUtils.getSuccess(contentUploadDTOS);
    }

    @PostMapping("queryProductOrderList")
    @ApiOperation(value = "queryProductOrderList", notes = "查询成品码订单", produces = "application/json; charset=utf-8")
    public ResponseResult queryProductOrderList(@RequestBody LinkedHashMap<String,Object> map) throws Exception {
        List<JSONObject> result = null;
        try {
            result = this.rtChangeService.queryProductOrderList(map);
        } catch (Exception e) {
            return ResponseResult.fail("200", e.getMessage());
        }
        return ResponseResult.success(result);
    }

    @PostMapping("saveProductOrderList")
    @ApiOperation(value = "saveProductOrderList", notes = "成品BOM订单保存", produces = "application/json; charset=utf-8")
    public ResponseResult saveProductOrderList(@RequestBody LinkedHashMap<String,Object> map) throws Exception {
        try {
            return ResponseResult.success(this.rtChangeService.saveProductOrderList(map));
        } catch (Exception e) {
            return ResponseResult.fail("200", e.getMessage());
        }
    }

    @GetMapping("getOrgNameByUser")
    @ApiOperation(value = "getOrgNameByUser", notes = "获取用户所在部门", produces = "application/json; charset=utf-8")
    public ResponseResult getOrgNameByUser(String oid) throws Exception {
        String department = this.rtChangeService.getOrgNameByUserOid(oid);
        return ResponseResult.success(department);
    }

    @PostMapping("getChangeImpactMbom")
    @ApiOperation(value = "getChangeImpactMbom", notes = "获取受影响的MBOM", produces = "application/json; charset=utf-8")
    public ResponseResult getChangeImpactMbom(@RequestBody LinkedHashMap<String,Object> map) throws Exception {
        JSONObject result = this.rtChangeService.getChangeImpactMbom(map);
        return ResponseResult.success(result);
    }


    @PostMapping("saveChangeImpactMbom")
    @ApiOperation(value = "saveChangeImpactMbom", notes = "保存受影响的MBOM", produces = "application/json; charset=utf-8")
    public ResponseResult saveChangeImpactMbom(@RequestBody LinkedHashMap<String,Object> map) throws Exception {
        try {
            return ResponseResult.success(this.rtChangeService.saveChangeImpactMbom(map));
        } catch (Exception e) {
            return ResponseResult.fail("200", e.getMessage());
        }
    }

    @PostMapping("getSaveChangeImpactMbom")
    @ApiOperation(value = "getSaveChangeImpactMbom", notes = "获取已保存受影响的MBOM", produces = "application/json; charset=utf-8")
    public ResponseResult getSaveChangeImpactMbom(@RequestBody LinkedHashMap<String,Object> map) throws Exception {
        JSONObject result = this.rtChangeService.getSaveChangeImpactMbom(map);
        return ResponseResult.success(result);
    }
    @PostMapping("lockProductOrder")
    @ApiOperation(value = "lockProductOrder", notes = "锁定成品码订单", produces = "application/json; charset=utf-8")
    public ResponseResult lockProductOrder(@RequestBody LinkedHashMap<String,Object> map) throws Exception {
        try {
            return ResponseResult.success(this.rtChangeService.lockProductOrder(map));
        } catch (Exception e) {
            return ResponseResult.fail("200", e.getMessage());
        }
    }

    @GetMapping("getLockProductOrder")
    @ApiOperation(value = "getLockProductOrder", notes = "查看锁定成品码订单", produces = "application/json; charset=utf-8")
    public ResponseResult getLockProductOrder(String changeNo, String partNumber, String productCode) throws Exception {
        try {
            return ResponseResult.success(this.rtChangeService.getLockProductOrder(changeNo, partNumber,productCode));
        } catch (Exception e) {
            return ResponseResult.fail("200", e.getMessage());
        }
    }
}
